package com.orangelife.mobile.goodsrent.biz;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.jump.CommonJumper;
import com.common.sp.UserSp;
import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.StringUtil;
import com.curry.android.view.BadgeView;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.biz.PullListViewImpl;
import com.orangelife.mobile.common.view.ShopCartAnim;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.confirmOrder.activity.GoodsRentConfirmOrderActivity;
import com.orangelife.mobile.goodsrent.adapter.RentAdapter;
import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.EditText.EditTextChangeClick;
import com.orangelife.mobile.widget.GoodsRentPopupWindow;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/21.
 */
public abstract class GoodsListImpl extends PullListViewImpl implements GoodsInfoCallBack {
    private static final int RENT = 0;
    private GoodsInfo mGoodsInfo;
    protected BadgeView mBadgeView;
    private GoodsRentPopupWindow goodsRentPopupWindow;
    private String mDay = "1";
    private EditTextChangeClick editTextChangeClick;
    private List<List<GoodsInfo>> mList;
    private List<List<GoodsInfo>> mRefreshList;
    private EditText etSearchGoods;
    private PullToRefreshListView lvGoods;
    private RentAdapter mAdapter;

    @Override
    public List<GoodsInfo> getGoodsList(List<Map<String, Object>> mapList) {
        List<GoodsInfo> goodsInfos = new ArrayList<>();
        for (int i = 0; i < mapList.size(); i++) {
            mGoodsInfo = new GoodsInfo();
            mGoodsInfo.setrGoodID(mapList.get(i).get("goodsID").toString());
            mGoodsInfo.setGoodName(mapList.get(i).get("goodsName").toString());
            mGoodsInfo.setGoodImgs(mapList.get(i).get("goodsImgs").toString());
            mGoodsInfo.setDispatchType(mapList.get(i).get("dispatchType").toString());
            mGoodsInfo.setGoodsBrief(mapList.get(i).get("goodsBrief").toString());
            mGoodsInfo.setGoodsLogo(mapList.get(i).get("goodsLogo").toString());
            mGoodsInfo.setMerID(mapList.get(i).get("merID").toString());
            mGoodsInfo.setPdtDesc(mapList.get(i).get("pdtDesc").toString());
            mGoodsInfo.setDay(mDay);
            mGoodsInfo.setUnit("天");
            mGoodsInfo.setPrice(mapList.get(i).get("price").toString());
            goodsInfos.add(mGoodsInfo);
        }
        return goodsInfos;
    }

    protected void request(boolean isRefresh, PullToRefreshListView pullListView, FatherAdapter fatherAdapter) {
        setRefreshAndLoad(isRefresh, pullListView, fatherAdapter);
    }

    @Override
    public List<List<GoodsInfo>> dismanting(List<GoodsInfo> listmap) {
        List<GoodsInfo> list1 = new ArrayList<>();
        List<List<GoodsInfo>> list2 = new ArrayList<>();
        if (listmap != null) {
            for (int i = 0; i < listmap.size(); i++) {
                list1.add(listmap.get(i));
                if (i % 2 > 0) {
                    list2.add(list1);
                    list1 = new ArrayList<>();
                }
                if (i == listmap.size() - 1 && i % 2 == 0) {
                    list2.add(list1);
                }

            }
        } else {
            return null;
        }
        return list2;
    }

    protected void showPopupWindow(RelativeLayout relativeLayout) {
        if (mBadgeView != null) {
            if (mBadgeView.getBadgeCount() != 0) {
                /**查找出所有的租借物品信息*/
                List<GoodsInfo> goodsDaoList = GoodsDao.getInstance().selectUserInfoList();
                goodsRentPopupWindow = new GoodsRentPopupWindow(goodsDaoList, this, relativeLayout);
            }
        }
    }

    protected void initShoppingCardNum(ImageButton ibShoppingCard) {
        setShoppingCardNum(ibShoppingCard, GoodsDao.getInstance().getGoodsNum());
    }

    protected void initGoodsTotalPrice(TextView tvTotalPrice) {
        setGoodsTotalPrice(tvTotalPrice, GoodsDao.getInstance().getGoodsTotalPrice());
    }

    /**
     * 设置物品总价
     */
    protected void setGoodsTotalPrice(TextView tvTotalPrice, String totalPrice) {
        tvTotalPrice.setText(Constant.RMB_FLAG + totalPrice);
    }


    /**
     * 设置购物车物品数量
     *
     * @param num
     */
    protected void setShoppingCardNum(ImageButton ibShoppingCard, int num) {
        if (mBadgeView == null) {
            mBadgeView = new BadgeView(this);
        }
        mBadgeView.setBadgeCount(num);
        mBadgeView.setTargetView(ibShoppingCard);

        if (num == 0 && goodsRentPopupWindow != null) {
            if (goodsRentPopupWindow.isShowing()) {
                goodsRentPopupWindow.dismiss();
            }
        }
    }

    protected void jumpNewActivity() {
        if (mBadgeView != null) {
            if (mBadgeView.getBadgeCount() != 0) {
                /**是否身份认证*/
                if (!StringUtil.isContainBlank(UserSp.getAccessToken())) {
                    ((CurryApplication) this.getApplicationContext()).setShopping(true);
                    IntentHelper.getInstances().getIntent(GoodsListImpl.this, GoodsRentConfirmOrderActivity.class);
                } else {
                    CommonJumper.jumpToLoginWhenExitWithClear(GoodsListImpl.this);
                }
            } else {
                ToastHelper.getInstance()._toast("请添加物品");
            }
        }
    }

    public void startShopCardAnim(View view, ImageButton ibShoppingCard) {
        ShopCartAnim.getInstance().setGoodsInfoCallBack(this);
        ShopCartAnim.getInstance().initView(this, ibShoppingCard, view);
    }

    @Override
    protected void requestList(int flags, int pageNo) {
        getShopList(pageNo, pageSize);
    }

    /**
     * 获得商家信息列表
     */
    protected void getShopList(int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", pageNo);
        map.put("pageSize", pageSize);
        map.put("shelves", "1");
        map.put("search", etSearchGoods.getText().toString());
        JSONRequest.getInstance().sendRequest(JSONRequest.POST, Constant.URL_RENT_LIST, map, handler, RENT, HttpUtil.ACTION_JSON);
    }

    protected void pullListView(PullToRefreshListView lvGoods, RentAdapter mAdapter, EditText etSearchGoods, List<List<GoodsInfo>> mRefreshList) {
        this.etSearchGoods = etSearchGoods;
        this.mRefreshList = mRefreshList;
        this.lvGoods = lvGoods;
        this.mAdapter = mAdapter;
        pullListView(lvGoods, handler);
    }

    @Override
    protected List<List<GoodsInfo>> listData() {
        return mRefreshList;
    }

    protected void onEditTextChangeClick(EditText etSearchGoods) {
        if (editTextChangeClick == null) {
            editTextChangeClick = new EditTextChangeClick();
        }
        editTextChangeClick.setEditTextChange(etSearchGoods, handler);
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            DialogHelper.closeDialog(dialog);
            switch (msg.what) {
                case 500:
                    ToastHelper.getInstance()._toast(
                            getResources().getString(
                                    R.string.net_new_request));
                    DialogHelper.closeDialog(dialog);

                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                case RENT:
                    List<Map<String, Object>> mapList = (List<Map<String, Object>>) JSONHelper.jsonToMap(msg.obj.toString()).get("pageEntity");
                    mList = dismanting(getGoodsList(mapList));
                    updateListView(mList, mRefreshList, lvGoods, mAdapter);
                    break;
                case EditTextChangeClick.RENT_SEARCH:
                    getShopList(Constant.PAGE_NO_DEFAULT, pageSize);
                    break;
                default:
                    break;
            }
        }
    };
}
